package com.school.aftersalemanagement.controller.ui.bicyclenumopen;

import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.school.aftersalemanagement.R;
import com.school.aftersalemanagement.controller.ui.scancode.ScanCodeActivity;
import com.school.aftersalemanagement.global.BaseToolBarActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class BicycleNumberOpenActivity extends BaseToolBarActivity {
    @BindView(R.id.bicyclenumberopen_inputnumber)
    EditText inputnumber;

    private CameraManager manager;
    private Camera camera = null;
    private Camera.Parameters parameters = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bicyclenumberopen);
        setToolbarText("号码开锁");
        manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    }


    /**
     * 打开闪光灯
     *
     * @return
     */
    private void open() {
        try {
            camera = Camera.open();
            camera.startPreview();
            Camera.Parameters parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 关闭闪光灯
     *
     * @return
     */
    private void close() {
        try {
            Camera.Parameters parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
            camera.release();
            camera = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    public static boolean isOpen = false;
    @OnClick({ R.id.bicyclenumberopen_openflashlight,R.id.bicyclenumberopen_scancode,R.id.bicyclenumberopen_now})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bicyclenumberopen_openflashlight:
                if (!isOpen) {
                    open();
                    isOpen = true;
                } else {
                    close();
                    isOpen = false;
                }
                break;
            case R.id.bicyclenumberopen_scancode:
                finish();
                break;
            case R.id.bicyclenumberopen_now:
                String bikeNum = inputnumber.getText().toString();
                Intent resultIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("bikeNum", bikeNum);
                resultIntent.putExtras(bundle);
                BicycleNumberOpenActivity.this.setResult(RESULT_OK, resultIntent);
                BicycleNumberOpenActivity.this.finish();
                break;
        }
    }


}
